//
//  ViewController.m
//  20202
//
//  Created by student on 16/3/16.
//  Copyright © 2016年 niit. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UITextView *textview;
@property (strong,nonatomic) NSMutableArray *stuList;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateInfo) name:UIApplicationWillEnterForegroundNotification object:nil];
    
   [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateInfo2) name:UIApplicationWillResignActiveNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateInfo3) name:UIApplicationDidEnterBackgroundNotification object:nil];
    
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateInfo4) name:UIApplicationDidBecomeActiveNotification object:nil];
}
    

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    NSUserDefaults *d = [NSUserDefaults standardUserDefaults];
    float redColor = [d floatForKey:@"Red"]/255.0;
    float BlueColor = [d floatForKey:@"Blue"]/255.0;
    float GreenColor = [d floatForKey:@"Green"]/255.0;
    self.view.backgroundColor = [UIColor colorWithRed:redColor green:GreenColor blue:BlueColor alpha:255/255.0];
    
}


- (NSMutableArray *)stuList
{
    if(_stuList == nil)
    {
        // 判断存档文件是否存在，如果之前已有存档文件，则从存档中读取出来以前的数据
        NSFileManager *fm = [NSFileManager defaultManager];
        if([fm fileExistsAtPath:[self plistPath]])
        {
            _stuList = [NSMutableArray arrayWithContentsOfFile:[self plistPath]];
        }
        else
        {
            // 没有存档文件则新建一个空数组
            _stuList = [NSMutableArray array];
        }
    }
    return _stuList;
}

- (NSString *)plistPath
{
    NSString *docPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *filePath = [docPath stringByAppendingPathComponent:@"Datas.plist"];
    //    NSLog(@"存储位置:%@",filePath);
    return filePath;
}

- (void)dealloc
{
    // 移除通知(必要!)
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)savePlist
{
    // 写入到文件
    [self.stuList writeToFile:[self plistPath] atomically:YES];
}


- (void)updateInfo
{
    NSDate *date = [NSDate date];

    NSDateFormatter *f = [[NSDateFormatter alloc] init];
    [f setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *str = [f stringFromDate:date];
    NSString *s = [NSString stringWithFormat:@"%@,将进入前台",str];
    [self.stuList addObject:s];
    [self savePlist];
    [self reload];
}

- (void)updateInfo2
{
    NSDate *date = [NSDate date];
    
    NSDateFormatter *f = [[NSDateFormatter alloc] init];
    [f setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *str = [f stringFromDate:date];
    NSString *s = [NSString stringWithFormat:@"%@,将取消活动",str];
    [self.stuList addObject:s];
    [self savePlist];
    [self reload];

}


- (void)updateInfo3
{
    NSDate *date = [NSDate date];
    
    NSDateFormatter *f = [[NSDateFormatter alloc] init];
    [f setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *str = [f stringFromDate:date];
    NSString *s = [NSString stringWithFormat:@"%@,已经进入后台",str];
    [self.stuList addObject:s];
    [self savePlist];
    [self reload];
    
}

- (void)updateInfo4
{
    NSDate *date = [NSDate date];
    
    NSDateFormatter *f = [[NSDateFormatter alloc] init];
    [f setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *str = [f stringFromDate:date];
    NSString *s = [NSString stringWithFormat:@"%@,已经激活",str];
    [self.stuList addObject:s];
    [self savePlist];
    [self reload];
    
}

- (void)reload
{
    
    for (int i = 0; i<self.stuList.count; i++)
    {
        self.textview.text = [NSString stringWithFormat:@"%@",self.stuList[i]];
        NSLog(@"%@",self.stuList[i]);

}
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
